package dst.graph.explorer;

import dst.graph.GraphSearchable;

/**
 * A TreeExplorer for searching for a data contained in a Tree.
 * Returns the node in the tree with the given data if found, returns
 * null otherwise.
 *
 * Fully Tested: No
 * Fully Documented:
 * Created: Nov 12, 2011
 * Last Updated: Nov 12, 2011
 *
 * @author Shimu Wu
 */
public class SearchExplorer<T> implements TreeExplorer<T> {

    private T searchData;
    private GraphSearchable<T> returnNode;

    /**
     * Instantiates a SearchExplorer that searches a Tree for
     * a GraphNode that contains the given searchData.
     *
     * @param searchData the data to be searched for in the Tree
     */
    public SearchExplorer (T searchData) {
        this.searchData = searchData;
    }

    /**
     * Returns true if the given node contains the searchData (in which
     * case the given node also becomes the return node for getReturnNode()),
     * false otherwise.
     *
     * @param node the node to be checked for the searchData
     * @return true if the given node contains the searchData, false otherwise
     */
    @Override
    public boolean exploreCurrent(GraphSearchable<T> node) {
        if (node.hasData(searchData)){
            returnNode = node;
            return true;
        } else {
            return false;
        }
    }

    /**
     * Returns the node with the searchData, null if no node
     * was found with the searchData
     *
     * @return the node with the searchData.
     */
    public GraphSearchable<T> getReturnNode() {
        return returnNode;
    }
}
